package com.um.jdy.platform.device.controller;

import com.um.jdy.common.device.entity.dto.PileDTO;
import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.common.device.entity.vo.PileVO;
import com.um.jdy.common.device.service.PileService;
import com.um.jdy.platform.device.entity.dto.*;
import com.um.jdy.platform.device.entity.vo.PileErrorVO;
import com.um.jdy.platform.device.entity.vo.PileItemVO;
import com.um.jdy.platform.device.manger.PileManager;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.RequestJson;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BasePlusController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 控制器：充电桩表
 */
@RestController
@RequestMapping("/api/v1/cms/device/pile")
@Api(tags = "设备管理", description = "充电桩表")
@ApiPermission("pile")
@TokenRequired
@Validated
public class PileController extends BasePlusController<PileManager, PileService, Pile, PileDTO, PileVO,PilePageDTO, PileListDTO> {

    /**
     * 分页查询设备库存
     * @param params
     * @return
     */
    @ApiPermission("storage_page")
    @ApiOperation("分页查询设备库存")
    @PostMapping("/storage_page")
    public Response<Page<PileVO>> getPileStoragePage(@RequestBody PilePageDTO params){
        PageParams pageParams = params.toPageParams(new String[]{"name","code","imei","deviceType"},new String[]{"d.name","d.code","d.imei","d.device_type"});
        return manager.getPileStoragePage(pageParams);
    }

    /**
     * 分页查询待运营设备
     * @param params
     * @return
     */
    @ApiPermission("shop_page")
    @ApiOperation("分页查询待运营设备")
    @PostMapping("/shop_page")
    public Response<Page<PileItemVO>> getPendingPilePage(@RequestBody AgentPilePageDTO params){
        PageParams pageParams = params.toPageParams(new String[]{"name","code","imei","deviceType","platformMerchantId"},
                new String[]{"d.name","d.code","d.imei","d.device_type","d.platform_merchant_id"});
        return manager.getPendingPilePage(pageParams);
    }

    /**
     * 分页查询运营设备
     * @param params
     * @return
     */
    @ApiPermission("pile_page")
    @ApiOperation("分页查询运营设备")
    @PostMapping("/pile_page")
    public Response<Page<PileItemVO>> getUsePilePage(@RequestBody UsePilePageDTO params){
        PageParams pageParams = params.toPageParams(new String[]{"name","code","imei","deviceType","platformMerchantId","deviceStationId","deviceStatus"},
                new String[]{"d.name","d.code","d.imei","d.device_type","d.platform_merchant_id","d.device_station_id","d.device_status"});
        return manager.getPilePage(pageParams);
    }

    /**
     * 首页查询设备列表-不需要权限控制
     * @param params
     * @return
     */
    @ApiOperation("首页查询设备列表")
    @PostMapping("/home_page")
    public Response<Page<PileItemVO>> getHomePage(@RequestBody UsePilePageDTO params){
        PageParams pageParams = params.toPageParams(new String[]{"name","code","imei","deviceType","platformMerchantId","deviceStationId","deviceStatus"},
                new String[]{"d.name","d.code","d.imei","d.device_type","d.platform_merchant_id","d.device_station_id","d.device_status"});
        return manager.getPilePage(pageParams);
    }

    /**
     * 下发到运营商
     * @param deliveryDTO
     * @return
     */
    @ApiPermission("to_merchant")
    @ApiOperation("下发到运营商")
    @PostMapping("arrange_to_merchant")
    public Response arrangeToMerchant(@Valid @RequestBody DeliveryDTO deliveryDTO) {
        manager.arrangeToMerchant(deliveryDTO);
        return Response.success();
    }

    /**
     * 下发到站场
     * @param deliveryDTO
     * @return
     */
    @ApiPermission("to_station")
    @ApiOperation("下发到站场")
    @PostMapping("arrange_to_station")
    public Response arrangeToStation(@Valid @RequestBody DeliveryDTO deliveryDTO) {
        manager.arrangeToStation(deliveryDTO);
        return Response.success();
    }

    /**
     * 设备注册
     * @param devicePileIdDTO
     * @return
     */
    @ApiPermission("register")
    @ApiOperation("设备注册")
    @PostMapping("register")
    public Response register(@Valid @RequestBody DevicePileIdDTO devicePileIdDTO) {
        manager.register(devicePileIdDTO.getId());
        return Response.success();
    }

    /**
     * 重启设备
     * @param codeParamsDTO
     * @return
     */
    @ApiPermission("restart")
    @ApiOperation("重启设备")
    @PostMapping("restart")
    public Response restart(@Valid @RequestBody CodeParamsDTO codeParamsDTO) {
        manager.restart(codeParamsDTO.getCode());
        return Response.success();
    }

    /**
     * 设备回收
     * @param codeParamsDTO
     * @return
     */
    @ApiPermission("recover")
    @ApiOperation("设备回收")
    @PostMapping("recover")
    public Response recover(@Valid @RequestBody CodeParamsDTO codeParamsDTO) {
        manager.recover(codeParamsDTO.getCode());
        return Response.success();
    }

    /**
     * 设备批量回收
     * @param codesParamsDTO
     * @return
     */
    @ApiPermission("recover_all")
    @ApiOperation("设备批量回收")
    @PostMapping("recover_all")
    public Response recoverAll(@Valid @RequestBody CodesParamDTO codesParamsDTO) {
        manager.recoverAll(codesParamsDTO.getCodes());
        return Response.success();
    }

    /**
     * 设备换码
     * @param changeCodeDTO
     * @return
     */
    @ApiPermission("change_code")
    @ApiOperation("设备换码")
    @PostMapping("change_code")
    public Response changeCode(@Valid @RequestBody ChangeCodeDTO changeCodeDTO) {
        manager.changeCode(changeCodeDTO);
        return Response.success();
    }

    /**
     * 设备转移
     * @param transferDTO
     * @return
     */
    @ApiPermission("transfer")
    @ApiOperation("设备转移")
    @PostMapping("transfer")
    public Response transfer(@Valid @RequestBody TransferDTO transferDTO) {
        manager.transfer(transferDTO);
        return Response.success();
    }

    /**
     * 批量更换运营商
     * @param exchangeDTO
     * @return
     */
    @ApiPermission("exchange")
    @ApiOperation("批量更换运营商")
    @PostMapping("exchange")
    public Response exchange(@Valid @RequestBody ExchangeDTO exchangeDTO) {
        manager.exchange(exchangeDTO);
        return Response.success();
    }

    /**
     * 导入excel
     *
     * @param params
     */
    @ApiPermission("import")
    @ApiOperation("导入excel")
    @PostMapping("/import")
    public Response<List<PileErrorVO>> importExcel(@RequestBody @Valid ImportPileParams params){
        return manager.importExcel(params);
    }

    /**
     * 固件升级
     * @param params
     * @return
     */
    @ApiPermission("upgrade")
    @ApiOperation("固件升级")
    @PostMapping("upgrade")
    public Response upgrade(@RequestBody @Valid UpgradeParams params){
        return manager.upgrade(params);
    }

    /**
     * 设置计费模型
     * @param code
     * @return
     */
    @ApiPermission("model")
    @ApiOperation("设置计费模型")
    @PostMapping("set_price_model")
    public Response setPriceModel(@RequestJson @NotNull String code){
        return manager.setPriceModel(code);

    }

    /**
     * 同步时间
     * @param code
     * @return
     */
    @ApiPermission("update_time")
    @ApiOperation("同步时间")
    @PostMapping("update_time")
    public Response updateTime(@RequestJson @NotNull String code){
        return manager.updateTime(code);
    }

    /**
     * 重启设备
     * @param params
     * @return
     */
    @ApiPermission("reset")
    @ApiOperation("重启设备")
    @PostMapping("reset")
    public Response reset(@RequestBody RestartParams params){
        return manager.reset(params);
    }

    /**
     * 发送欣华自定义指令
     * @param params
     * @return
     */
    @ApiPermission("send_cmd")
    @ApiOperation("发送欣华自定义指令")
    @PostMapping("send_cmd")
    public Response sendCmd(@RequestBody SendCmdParams params){
        return manager.sendCmd(params);
    }
}
